package com.hotel.tv.web.admin.controllers;

import com.autumn.application.dto.input.DefaultAdvancedQueryInput;
import com.autumn.web.annotation.IgnoreApiResponseBody;
import com.autumn.web.controllers.AbstractEditController;
import com.autumn.web.utils.ControllerUtils;
import com.autumn.zero.file.storage.FileStorageUtils;
import com.autumn.zero.file.storage.application.dto.TemporaryFileInformationDto;
import com.autumn.zero.file.storage.application.services.FileUploadManager;
import com.hotel.tv.application.admin.dto.order.OrderDetailsOutput;
import com.hotel.tv.application.admin.dto.order.OrderInput;
import com.hotel.tv.application.admin.dto.order.OrderListOutput;
import com.hotel.tv.application.admin.services.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 * </p>
 *
 * @description TODO
 * @author: 老码农
 * @create: 2020-04-03 12:06
 **/
@RestController
@RequestMapping("/order")
@Api(tags = "订单管理")
@RequiresUser
public class OrderController extends AbstractEditController<OrderInput, OrderInput, OrderListOutput, OrderDetailsOutput, OrderService> {

    private final FileUploadManager fileUploadManager;

    /**
     * 实例化
     *
     * @param service           服务
     * @param fileUploadManager
     */
    public OrderController(OrderService service, FileUploadManager fileUploadManager) {
        super(service);
        this.fileUploadManager = fileUploadManager;
    }

    /**
     * @param input
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存订单")
    public OrderDetailsOutput save(@Valid @RequestBody OrderInput input) {
        return this.service.save(input);
    }

    /**
     * 查询订单
     *
     * @return
     */
    @PostMapping("/orders")
    @ApiOperation(value = "查询订单")
    public List<OrderListOutput> orders() {
        return this.service.orders();
    }

    /**
     * 下载Excel到响应体
     *
     * @param input
     * @throws UnsupportedEncodingException
     */
    @PostMapping("/download/excel/body")
    @ApiOperation(value = "下载Excel到响应体", produces = "application/octet-stream")
    @IgnoreApiResponseBody
    public StreamingResponseBody downloadForExcel(@RequestBody DefaultAdvancedQueryInput input,
                                                  HttpServletResponse response) throws UnsupportedEncodingException {
        return ControllerUtils.createExcelExportBody(this.service, input, response);
    }

    /**
     * 下载Excel到文件信息
     *
     * @param input 输入
     * @return
     * @throws Exception
     */
    @PostMapping("/download/excel/Info")
    @ApiOperation(value = "下载Excel到文件信息")
    public TemporaryFileInformationDto downloadForExcelInfo(@RequestBody DefaultAdvancedQueryInput input) throws Exception {
        return FileStorageUtils.createExcelTemporaryFile(this.service, fileUploadManager, input);
    }

    /**
     * 下载Excel导入模板文件
     *
     * @throws UnsupportedEncodingException
     */
    @PostMapping("/download/import/template/body")
    @ApiOperation(value = "下载Excel导入模板文件到响应体", produces = "application/octet-stream")
    @IgnoreApiResponseBody
    public StreamingResponseBody downloadImportTemplateByExcel(HttpServletResponse response) throws UnsupportedEncodingException {
        return ControllerUtils.createExcelImportTemplateBody(this.service, response);
    }

    /*@PostMapping("/download/import/template/Info")
    @ApiOperation(value = "下载到Excel导入模板文件信息")
    public TemporaryFileInformationDto downloadImportTemplateByExcelInfo() {
        return this.service.excelImportTemplateForFileInformation();
    }*/

    /**
     * Excel上传并导入
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload/import/excel")
    @ApiOperation(value = "Excel上传并导入")
    public int dataImportByExcel(@Valid @RequestParam("file") MultipartFile file) throws IOException {
        return this.service.excelImport(file.getInputStream(), null);
    }


}
